package com.hm.PushCaseService.work;

import com.hm.PushCaseService.pojo.CaseBean;
import com.hm.PushCaseService.pojo.response.PushLogBean;
import com.hm.PushCaseService.service.push.log.IPush;
import com.hm.PushCaseService.service.push.qycg.login.ILoginCookie;
import com.hm.PushCaseService.service.push.qycg.push.IPushCase;
import com.hm.PushCaseService.service.push.qycg.push.PushCaseListen;
import com.hm.PushCaseService.utils.time.DateTimeUtils;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * 推送到清远系统
 */
@Component
public class CasePushSystemReceiver {

    //鉴权
    private final ILoginCookie iLoginCookie;
    //时间戳
    private final IPushCase iPushCase;
    //推送记录
    private final IPush iPush;

    public CasePushSystemReceiver(IPush iPush, ILoginCookie iLoginCookie, IPushCase iPushCase) {
        this.iLoginCookie = iLoginCookie;
        this.iPushCase = iPushCase;
        this.iPush = iPush;
    }

    //订阅案件主题
    @JmsListener(destination = "caseInfoTopic")
    public void putCase(CaseBean caseBean) {
        String cookie = iLoginCookie.getCookie();
        if (cookie == null || cookie.isEmpty()) {
            PushLogBean bean = new PushLogBean();
            bean.eventCode = caseBean.eventcode;
            bean.responseState = "失败";
            bean.pushState = "cookie获取失败";
            bean.pushTime = DateTimeUtils.getCurrentTime();
            iPush.addPushLog(bean);
            return;
        }
        iPushCase.push(caseBean, cookie, new PushCaseListen() {
            @Override
            public void pushSucceed(String eventCode) {
                PushLogBean bean = new PushLogBean();
                bean.eventCode = eventCode;
                bean.responseState = "成功";
                bean.pushState = "推送成功";
                bean.pushTime = DateTimeUtils.getCurrentTime();
                iPush.addPushLog(bean);
            }

            @Override
            public void pushFailure(String eventCode, String errMessage) {
                PushLogBean bean = new PushLogBean();
                bean.eventCode = eventCode;
                bean.responseState = "失败";
                bean.pushState = errMessage;
                bean.pushTime = DateTimeUtils.getCurrentTime();
                iPush.addPushLog(bean);
            }
        });
    }
}
